package com.zhangfei.function;

import java.util.Comparator;
import java.util.TreeSet;

public class ComparatorTest {

    private static Comparator<Integer> comparator() {
        return new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };
    }

    //  简单写法
    private static Comparator<Integer> comparator1() {
        return (Integer o1, Integer o2) -> o2.compareTo(o1);
    }

    public static void main(String[] args) {
        //匿名内部类
        Comparator<Integer> cp1 = comparator();

        TreeSet<Integer> set = new TreeSet<>(cp1);
        set.add(1);
        set.add(3);
        set.add(2);
        set.add(100);
        System.out.println(set);
        System.out.println("===========");



        //lambda
        Comparator<Integer> cp2 = comparator1();

        TreeSet<Integer> set1 = new TreeSet<>(cp2);
        set1.add(1);
        set1.add(3);
        set1.add(2);
        set1.add(100);
        System.out.println(set1);
        System.out.println("===========");



        //类型检查  类型推断

        //无类型推断
        Comparator<Integer> cp3 = (Integer o1, Integer o2) -> o2.compareTo(o1);

        //有类型推断
        Comparator<Integer> cp4 = ( o1,  o2) -> o2.compareTo(o1);
    }
}
